package com.bw.zg3park2212.park

import android.graphics.drawable.GradientDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.ParkTypeEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.ParkTyprAdpter
import com.bw.zg3park2212.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/ParkActivity")
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {
    lateinit var parkTyprAdpter: ParkTyprAdpter
    var addid="-1"
    var type=""
    var index=1
    override fun intitView() {
        parkTyprAdpter= ParkTyprAdpter()
        binding.rvCwgl.adapter=parkTyprAdpter
        binding.rvCwgl.layoutManager=LinearLayoutManager(this)

        binding.addPark.setOnClickListener {
            ARouter.getInstance().build("/app/ParkAddActivity").navigation()
        }
        binding.shai.setOnClickListener {
            binding.ct.openDrawer(GravityCompat.END)
        }
        binding.drawRg.setOnCheckedChangeListener { group, checkedId ->
            binding.ct.closeDrawer(GravityCompat.END)
            var staus=""
            if (checkedId==R.id.rb_kx){
                staus="空闲"
            }else if (checkedId==R.id.rb_zy){
                staus="已占用"
            }else{
                staus="维护中"
            }
            lifecycleScope.launch {
                viewmodel.getParkType(staus,"状态").collect{
                    parkTyprAdpter.submitData(it)
                }
            }
        }
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                addid=query!!
                lifecycleScope.launch {
                    viewmodel.getParkType(addid,type).collect{
                        parkTyprAdpter.submitData(it)
                    }
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
        binding.drawArea.setOnClickListener {
            binding.ct.closeDrawer(GravityCompat.END)
            type="区域"
            if (addid=="-1"){
                ToastUtils.showLong("请填写区域id")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewmodel.getParkType(addid,type).collect{
                    parkTyprAdpter.submitData(it)
                }
            }
        }
        binding.drawType.setOnClickListener {
            binding.ct.closeDrawer(GravityCompat.END)
            type="类型"
            if (addid=="-1"){
                ToastUtils.showLong("请填写类型")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewmodel.getParkType(addid,type).collect{
                    parkTyprAdpter.submitData(it)
                }
            }
        }
        binding.drawCarNo.setOnClickListener {
            binding.ct.closeDrawer(GravityCompat.END)
            type="车牌号"
            if (addid=="-1"){
                ToastUtils.showLong("请填写车牌号")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewmodel.getParkType(addid,type).collect{

                    parkTyprAdpter.submitData(it)
                    Log.d("sss","1111")

                }
            }
        }
        parkTyprAdpter.setOnItemClickListener(object :ParkTyprAdpter.OnItemClickListener{
            override fun onItemClick(position: Int, item: ParkTypeEntityItem) {
                ARouter.getInstance().build("/app/ParkUpdetActivity")
                    .withSerializable("bean",item)
                    .navigation()
            }
        })
        parkTyprAdpter.setOnItemClickLongListener(object :ParkTyprAdpter.OnItemClickLongListener{
            override fun onItemLongClick(position: Int, item: ParkTypeEntityItem) {
                //index=position
                viewmodel.parkdel(item.id)
            }
        })

        //左滑删除
//        ItemTouchHelper(object :ItemTouchHelper.Callback(){
//            override fun getMovementFlags(
//                recyclerView: RecyclerView,
//                viewHolder: RecyclerView.ViewHolder,
//            ): Int {
//                return makeMovementFlags(0,ItemTouchHelper.LEFT)
//            }
//
//            override fun onMove(
//                recyclerView: RecyclerView,
//                viewHolder: RecyclerView.ViewHolder,
//                target: RecyclerView.ViewHolder,
//            ): Boolean {
//                return true
//            }
//
//            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//                parkTyprAdpter.notifyItemRemoved(viewHolder.adapterPosition)
//                viewmodel.parkdel(viewHolder.itemView.id)
//
//            }
//        }).attachToRecyclerView(binding.rvCwgl)
    }

    override fun intitData() {
        viewmodel.parkdelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
            parkTyprAdpter.notifyItemRemoved(index-1)
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            viewmodel.getParkType("标准车位","类型").collect{
                parkTyprAdpter.submitData(it)
            }
        }
    }

}